package com.smart.model;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 菜单类
 * 
 * @author Sunxin
 *
 */

@Entity 
@Table(name = "Menu") 
public class Menu extends BaseEntity<Menu>{
	
	private static final long serialVersionUID = 1L;
	public static final String ROOT_MENU_CODE = "ROOT";
	/**
	 * 菜单名, 也是菜单项的文字; 
	 */
	private String name;

	/**
	 * 菜单key
	 */
	private String code;
	
	/**
	 * 菜单等级
	 */
	private int level = 0;
	/**
	 * 菜单超链接URL
	 */
	private String url;
	
	@ManyToOne
    @JoinColumn(name="parent_id")
	private Menu parent;
	
	/**
	 * 附加属性1
	 */
	private String attr1;
	
	/**
	 * 附加属性2
	 */
	private String attr2;
	
	/**
	 * 孩子节点列表
	 */
	@Transient
	private List<Menu> children;
	
	@Transient
	private String structuredName;
	
	/**
	 * 是否被选中
	 */
	@Transient
	private boolean selected = false;
	
	public String toString() {
		return "id: " + id + ", name: " + name + ", code: " + code + ", url: " + url;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Menu getParent() {
		return parent;
	}

	public void setParent(Menu parent) {
		this.parent = parent;
		if(parent.getName()!=null) {
			this.level = parent.getLevel() + 1;
		}
	}

	public List<Menu> getChildren() {
		return children;
	}

	public void setChildren(List<Menu> children) {
		this.children = children;
	}

	public String getAttr1() {
		return attr1;
	}

	public void setAttr1(String attr1) {
		this.attr1 = attr1;
	}

	public String getAttr2() {
		return attr2;
	}

	public void setAttr2(String attr2) {
		this.attr2 = attr2;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public String getStructuredName() {
		String blanks = "";
		for(int i=0; i<level*4; i++) {
			blanks += "&nbsp;";
		}
		return blanks + name;
	}

	public void setStructuredName(String structuredName) {
		this.structuredName = structuredName;
	}
	
	
}
